package com.lcx.myservice;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "lcx_debug";
    TextView tv;
    Button btn_bindservice;
    Button btn_unbindservice;
    Button btn_getdata;
    Intent intent;
    MyBindService mService;
    boolean isBind = false;

    ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected: ");
            MyBindService.MyBinder mBinder = (MyBindService.MyBinder)service;
            mService = mBinder.getService();
            //tv.setText("服务已绑定");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
        }
    };
    private void bindSerivce(View view) {
        Intent intent = new Intent(this,MyBindService.class);
        if(!isBind) {
            Toast.makeText(this, "bindService", Toast.LENGTH_SHORT).show();
            bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
            isBind = true;
        }
    }

    private void unBindSerivce(View view) {
        if (isBind) {
            Toast.makeText(this, "unbindService", Toast.LENGTH_SHORT).show();
            unbindService(mServiceConnection);
            isBind = false;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        init();
    }
    private void init(){
        btn_bindservice = findViewById(R.id.btn_bindservice);
        btn_unbindservice = findViewById(R.id.btn_unbindservice);
        btn_getdata = findViewById(R.id.btn_getdata);
        btn_bindservice.setOnClickListener((View.OnClickListener) this);
        btn_unbindservice.setOnClickListener((View.OnClickListener) this);
        btn_getdata.setOnClickListener((View.OnClickListener) this);
        tv = findViewById(R.id.textView);
        tv.setText("Hello World");
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        //intent = new Intent(this, MyIntentService.class);
        switch (v.getId()){
            case R.id.btn_bindservice:
                Log.d(TAG, "onClick:btn_bindservice ");
                bindSerivce(v);
                //intent.putExtra("num","1");
                break;
            case R.id.btn_unbindservice:
                Log.d(TAG, "onClick:btn_unbindservice ");
                unBindSerivce(v);
                //intent.putExtra("num","2");
                break;
            case R.id.btn_getdata:
                getDate();
                break;
        }
    }
    public void getDate() {
        if (mService == null)
            return;
        if (isBind) {
            int num = mService.getRamdomNumber();
            Log.d(TAG, "onClick:btn_getdata "+ num);
            Toast.makeText(this,"ramdom num: " + num,Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this,"please bind service first",Toast.LENGTH_SHORT).show();
        }

    }
}